package com.hl233.framework.controller;

import com.hl233.framework.utils.MinIOUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 张振强15065206763
 * @create 2025/4/24
 */
@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Autowired
    private MinIOUtil minIOUtil;

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "请选择要上传的文件";
        }
        try {
            String minioFilename = UUID.randomUUID().toString().replace("-", "") + "_" + file.getName();
            minIOUtil.uploadFileByMultipartFile(file,minioFilename);
            // 这里可以根据需要修改保存路径
//            String filePath = "uploads/" + file.getOriginalFilename();
//            File dest = new File(filePath);
//            if (!dest.getParentFile().exists()) {
//                dest.getParentFile().mkdirs();
//            }
//            file.transferTo(dest);
            return "文件上传成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "文件上传失败：" + e.getMessage();
        } catch (Exception e) {
            e.printStackTrace();
            return "文件上传失败：" + e.getMessage();
        }
    }

    @GetMapping("/test")
    public String test() {
        return "成功";
    }
}
